﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLTuyenXe.BLL;
using QLTuyenXe.DAL;
using QLTuyenXe.DAO;
using System.Collections;
using System.Data.SqlClient;
using QLTuyenXe.library;

namespace QLTuyenXe.GUI
{
    public partial class frmThemKH : DevComponents.DotNetBar.Office2007Form
    {
        BKhachHang kh = new BKhachHang();
        XuLyDL xl = new XuLyDL();
        ThongBao thongBao = new ThongBao();
        BUser user = new BUser();
        KHACHHANG khachHang = new KHACHHANG();
        public static string ten = string.Empty;
        public frmThemKH()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBoxEx.Show("Bạn thật sự muốn thoát không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtTenCT.Text != "" && txtDaiDien.Text != "" && txtChucVu.Text != "" && txtDiaChi.Text != "" && txtSDT.Text != "")
            {
                if (xl.nhom(txtTenCT.Text) == true && xl.HoTen(txtDaiDien.Text) == true && xl.HoTen(txtChucVu.Text) == true && xl.nhom(txtDiaChi.Text) == true && xl.IsNumber(txtSDT.Text)==true)
                {
                    DataTable tb = kh.Select_DK(txtTenCT.Text, txtDaiDien.Text, txtDiaChi.Text, txtSDT.Text);

                    if (tb.Rows.Count > 0)
                    {
                        thongBao.KiemTraTruocKhiThem();
                    }
                    else
                    {

                        int kq = 0;
                        khachHang.MaKH = kh.getOrder();
                        khachHang.DonviB = xl.ChuanHoa(txtTenCT.Text);
                        khachHang.DaidienB = xl.ChuanHoa(txtDaiDien.Text);
                        khachHang.ChucvuB = xl.ChuanHoa(txtChucVu.Text);
                        khachHang.DiachiB = xl.ChuanHoa(txtDiaChi.Text);
                        khachHang.DienthoaiB = txtSDT.Text;
                        if (Int64.Parse(txtSDT.Text) > 0)
                        {
                            kq = kh.Insert(khachHang);
                            if (kq > 0)
                            {
                                Clear();
                                txtMaDV.Text = kh.getOrder();
                                thongBao.Them();
                            }
                            else
                            {
                                thongBao.LoiThem();
                            }
                        }
                        else
                        {
                            thongBao.KiemTraSo();
                            //MessageBoxEx.Show("Số điện thoại không hợp lệ","thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                    }
                }
                else
                {
                    thongBao.KiemTraSo();
                }
            }
        }

        private void frmThemKH_Load(object sender, EventArgs e)
        {
            txtMaDV.Text = kh.getOrder();
        }
        public void Clear()
        {
            txtTenCT.Clear();
            txtDaiDien.Clear();
            txtChucVu.Clear();
            txtDaiDien.Clear();
            txtDiaChi.Clear();
            txtSDT.Clear();
        }

        private void txtSDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                thongBao.KiemTraSo();
            }
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void txtDaiDien_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.Handled = !((e.KeyChar >= 65 && e.KeyChar <= 122) || e.KeyChar == 8 || e.KeyChar == 32);  
        }
    }
}
